home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Essentials / MacApp Documentation / MacApp.TECH$ Archives / 1990 / Aug 90 / MacApp.Tech$ 8⁄10⁄90 / 1718-MemoryMgrProblem-Aug90 < prev    next >
Encoding:
Text File  |  1991-03-06  |  3.4 KB  |  99 lines  |  [TEXT/GEOL]

  1. Item    9093721                         8-Aug-90        18:00PDT
  2.  
  3. From:   AUST0334                        AUDev - CRIA, Canberra, ACT,IDV
  4.  
  5. To:     MACAPP.TECH$                    MacApp Technical
  6.  
  7. Sub:    MemoryMgrProblem
  8.  
  9. Re: Memory Management Problems
  10.  
  11. Dear Fellow MacAppers,
  12.  
  13. Recently I have been experiencing a few run-time bugs with our MacApp
  14. application.  Various pictures were being deleted when memory was tight etc.
  15. Upon reading the relevant documentation on memory management, the problem was
  16. clear.  These pictures and other data structures were going into MacApp
  17. temporary memory because the allocation was being performed by toolbox routines
  18. such as OpenPicture.  To fix the problem I wrote a couple of routines to copy
  19. the memory blocks in these data structures to MacApp permanent memory where
  20. they would not be deleted.
  21.  
  22. Having done this, I found that the pictures were still being deleted
  23. (presumably by MacApp).  All I end up with is a master pointer which contains a
  24. null indicating an EmptyHandle has been performed.  Although this is happening
  25. when memory is tight, I want the program to complain/fail on lack of free
  26. memory rather than releasing permanent blocks to make room.  It is almost like
  27. these blocks are still in temporary memory.  Has anyone else had this problem?
  28. I was under the impression the only way to do delete chunks of permanent memory
  29. was to explicity purge the handle yourself.
  30.  
  31. I have included the example case where I copy a PicHandle which should be in
  32. temporary memory into a block that should be in permanent memory:
  33.  
  34. ----------------------------------------------------------------------------
  35. TSomeCommand.DoIt;
  36.  begin
  37.   dstPic := OpenPicture(dstRect);
  38.   {make up picture here}
  39.   ClosePicture;
  40.   error := Temp2PermHandle(Handle(dstPic)); {never any errors}
  41.   SELF.fPicture := dstPic;  {fPicture is emptied when memory is tight}
  42.  end;
  43. ----------------------------------------------------------------------------
  44.  
  45.     Now for the functions:
  46.  
  47. ----------------------------------------------------------------------------
  48. function PermHandToHand (var h: Handle): OSErr;
  49. {functionally identical to HandToHand except returned handle is permanent}
  50.   var
  51.    newH: Handle;
  52.    error: OSErr;
  53.  begin
  54.   error := noErr;
  55.   if h^ = nil then
  56.    error := nilHandleErr;
  57.   if error = noErr then
  58.    begin
  59.     newH := NewPermHandle(GetHandleSize(h));    {allocate a perm block?}
  60.     if newH = nil then
  61.      error := memFullErr;
  62.     if error = noErr then
  63.      begin
  64.       HLock(Handle(h));
  65.       HLock(Handle(newH));
  66.       BlockMove(Ptr(h^), Ptr(newH^), GetHandleSize(h));     {copy block data}
  67.       HUnlock(Handle(h));
  68.       HUnlock(Handle(newH));
  69.      end;
  70.    end;
  71.   h := newH;    {return newH in h}
  72.   PermHandToHand := error;
  73.  end;
  74. ----------------------------------------------------------------------------
  75.  
  76. ----------------------------------------------------------------------------
  77. function Temp2PermHandle (var h: Handle): OSErr;
  78. {moves handle to permanent memory by copying and deleting original handle}
  79.   var
  80.    newH: Handle;
  81.    error: OSErr;
  82.  begin
  83.   newH := h;
  84.   error := PermHandToHand(newH); {get a permanent copy of h}
  85.   DisposHandle(h);               {get rid of h}
  86.   h := newH;                     {give back newH for h}
  87.   Temp2PermHandle := error;
  88.  end;
  89. ----------------------------------------------------------------------------
  90.  
  91. Martin Flanagan
  92. Techway Solutions
  93. Australia
  94.  
  95. Phone   +62 (06) 256 3415
  96. Fax     +62 (06) 251 5059
  97. Link    AUST0334
  98.  
  99.